package com.semac.client.aplicacao;

import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.Widget;

public abstract class AbstractPageController implements ValueChangeHandler<String>{		

	public AbstractPageController() {
		History.addValueChangeHandler(this);		
	}

	
	public void refresh(){
		History.fireCurrentHistoryState();
		
	}
	
	
	public void navegaParaPagina(Pagina pagina){
		History.newItem(pagina.getValorHistoryToken(), false);		
		this.vaiPraPagina(pagina.getView());
	}
	
	
	
	protected abstract void vaiPraPagina(Widget view);
	
	
	
	/**
	 * Esse eh o listener de quando o endereco do browser muda, ou seja, quando muda o 
	 * valor existente apos o # na URL 
	 */
	@Override
	public void onValueChange(ValueChangeEvent<String> event) {
		this.decidePagina(event.getValue());
	}
	
	
	
	/**
	 * pega a string apos o simbolo # na barra de endereco e decide qual pagina abrir
	 * @param pagina
	 */
	protected abstract void decidePagina(String pagina);
	

	
	
	public void removeHistoryToken() {
		History.newItem("", false);
	}
	
	
	
}
